﻿using System;
using System.Collections.Generic;
using XcoAppSpaces.Core.Ports.Remoting;

namespace XcoAppSpaces.Core.Ports.Utils
{
	/// <summary>
	/// Represents a single element in a port. Contains everything that is required to process the item.
	/// </summary>
	public interface IPortElement
	{
		/// <summary>
		/// The item that has been posted to the port, as an object.
		/// </summary>
		object ItemObj { get; }

		/// <summary>
		/// The list of causalities that was active when item has been posted.
		/// </summary>
		List<ICausality> Causalities { get; }

		/// <summary>
		/// Provides additional information about the message (e.g. address of sender), if it is a remote message.
		/// </summary>
		MessageContext MessageContext { get; set; }

		/// <summary>
		/// Signals that a message has been sent successfully (completes the sent completion source, posts an ack to the ack port).
		/// </summary>
		void SignalSent();

		/// <summary>
		/// Signals a processing error. This leads to the exception being posted to the exception port, or the TaskCompletionSource being marked as faulted.
		/// If no causality or TaskCompletionSource exists, then the error ist posted to the unhandled error port.
		/// </summary>
		/// <param name="ex">The excepction was thrown.</param>
		/// <returns>True if the error was handled by a causality or TaskCompletionSource, otherwise false.</returns>
		bool SignalError(Exception ex);
	}
}